package core;

import java.io.IOException;
import java.net.InetSocketAddress;

import log.ServerLogger;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import filter.KeepAliveMessageFactoryImpl;
import filter.KeepAliveRequestTimeoutHandlerImpl;
import filter.MyTextFactory;


/**
 * @see 服务器启动类
 * @author ganlinux
 * @date 2013年12月6日 09:23:31
 * @file MinaServer.java
 * @package com.minaqq.server
 * @project REMMS
 * @version 1.0
 * @since jdk1.6,mina 2.0
 */
public class MinaSocket {

	private static SocketAcceptor acceptor;
	private static MinaSocket instance;
	private boolean isStart=false;
	public MinaSocket() {}
	public static MinaSocket getInstance() {
		if(instance==null){
			instance=new MinaSocket();
		}
		return instance;
	}
	public static SocketAcceptor getAcceptor(){
		if(null==acceptor){
			// 创建非阻塞的server端的Socket连接
			acceptor = new NioSocketAcceptor();
		}
		return acceptor;
	}

	public boolean serverStart() {
		Global gpGlobal = Global.getInstance();

		boolean result=false;
		if(this.isStart()==false){
			DefaultIoFilterChainBuilder filterChain = getAcceptor().getFilterChain();
			// 添加编码过滤器 处理乱码、编码问题
			filterChain.addLast("codec", new ProtocolCodecFilter(new MyTextFactory()));
			// LoggingFilter loggingFilter = new LoggingFilter();
			//loggingFilter.setMessageReceivedLogLevel(LogLevel.INFO);
			//  loggingFilter.setMessageSentLogLevel(LogLevel.INFO);
			// 添加日志过滤器
			//filterChain.addLast("loger", loggingFilter);
			// 设置核心消息业务处理器
			getAcceptor().setHandler(new ServerMessageHandler());

			/*
        KeepAliveMessageFactory heartBeatFactory = new KeepAliveMessageFactoryImpl();
        KeepAliveRequestTimeoutHandler heartBeatHandler = new KeepAliveRequestTimeoutHandlerImpl();
        KeepAliveFilter heartBeat = new KeepAliveFilter(heartBeatFactory,IdleStatus.BOTH_IDLE, heartBeatHandler);
        // 是否回发 
        heartBeat.setForwardEvent(true);
        // 发送频率 
        heartBeat.setRequestInterval(HEARTBEATRATE);
        getAcceptor().getFilterChain().addLast("heartbeat", heartBeat);
			 */
			//getAcceptor().getSessionConfig().setBothIdleTime(30);
			// 设置session配置，30秒内无操作进入空闲状态
			getAcceptor().getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, gpGlobal.getHeartbeat());
			getAcceptor().getSessionConfig().setReadBufferSize(2048);
			getAcceptor().getSessionConfig().setReceiveBufferSize(2048);
			try {
				// 绑定端口9015
				getAcceptor().bind(new InetSocketAddress(gpGlobal.getListenPort()));
				this.setStart(true);
				result=true;

			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

	public int serverCount() 
	{
		return getAcceptor().getManagedSessionCount();
	}
	public int getSession() 
	{
		return 0;
		//return getAcceptor().getManagedSessions(2,IoSession.class.getId());
	}
	public boolean closeSocket() {
		getAcceptor().dispose(true);
		setStart(false);
		ServerLogger.INFO("关闭了服务器Socket");
		return false;
	}
	public boolean isStart() {
		return isStart;
	}

	public void setStart(boolean isStart) {
		this.isStart = isStart;
	}

}
